package uk.ac.ebi.pride.peptidome;

import org.apache.log4j.Logger;
import uk.ac.ebi.pride.jaxb.model.ModificationItem;
import uk.ac.ebi.pride.jaxb.model.Param;
import uk.ac.ebi.pride.jaxb.model.UserParam;
import uk.ac.ebi.pride.util.IOUtilities;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: rwang
 * Date: 20/05/11
 * Time: 10:48
 * To change this template use File | Settings | File Templates.
 */
public class UnimodReader {
    private static final Logger logger = Logger.getLogger(UnimodReader.class);

    public Map<String, ModificationItem> parse() {
        Map<String, ModificationItem> mods =  new HashMap<String, ModificationItem>();
        try {
            InputStream inputStream = UnimodReader.class.getClassLoader().getResourceAsStream("unimod.csv");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while((line = reader.readLine()) !=  null) {
                String[] parts = line.split(",");
                ModificationItem mod = new ModificationItem();
                mod.setModAccession(parts[0].trim());
                mod.setModDatabase("Unimod");
                mod.setModDatabaseVersion("");
                mod.getModMonoDelta().add(parts[2].trim());
                mod.getModAvgDelta().add(parts[3].trim());
                Param param = new Param();
                UserParam name = new UserParam();
                name.setName("Name");
                name.setValue(parts[1].trim());
                param.getUserParam().add(name);
                UserParam composition = new UserParam();
                composition.setName("Composition");
                composition.setValue(parts[4].trim());
                param.getUserParam().add(composition);
                mod.setAdditional(param);
                mods.put(parts[0].trim(), mod);
            }
        } catch (IOException e) {
            logger.error("Failed to read Unimod file", e);
        }
        return mods;
    }
}
